package leetcode;

import java.util.Stack;

/**
 * Created by Ghost on 2014/11/20 0020.
 */
public class Min_Stack {
    private Stack<Integer> stack = new Stack<Integer>();
    private Stack<Integer> minStack = new Stack<Integer>();
    public void push(int x) {
        stack.push(x);
        if(minStack.isEmpty())
            minStack.push(x);
        else{
            if(minStack.peek()>=stack.peek()){
                minStack.push(x);
            }
        }
    }

    public void pop() {
        if(stack.isEmpty())
            return;
        if(stack.peek()<=minStack.peek())
            minStack.pop();
        stack.pop();
    }

    public int top() {
        if(stack.isEmpty())
            return 0;
        return stack.peek();
    }

    public int getMin() {
        if(minStack.isEmpty())
            return 0;
        return minStack.peek();
    }
    public static void main(String[] args){
        Min_Stack min_stack = new Min_Stack();
        min_stack.push(5);
        min_stack.push(4);
        min_stack.push(3);
        min_stack.push(2);
        min_stack.push(1);
        min_stack.push(0);

        for(int i=0;i<7;i++){
            System.out.println(min_stack.top()+" "+min_stack.getMin()+" ");

            min_stack.pop();

        }

    }
}
